<?php

/**
 * @Author: 刘洪
 * @Date:   2018-11-22 11:05:06
 * @Last Modified by:   user
 * @Last Modified time: 2019-01-07 11:39:50
 */
namespace app\common\remind;

use think\Db;
use app\common\remind\RemindMain;

/**
 * 工单提醒类
 */
class RemindGongdan extends RemindMain
{
	
	/**
	 * 设置提醒类型为工单 3
	 * @var integer
	 */
	protected $remindType=3;

	/**
	 * 工单提醒主入口
	 * @return [type] [description]
	 */
	public function index()
	{
		 //初始化
		 $this->remindList=parent::init();
		 //数据重组
		 $this->regroupData();
		 //发送提醒
		 parent::send('工单');
	}

	/**
	 * 实现父类抽象方法
	 * @access protected
	 * @return [type] [description]
	 */
	protected function regroupData()
	{
		//arr[]=[trigger,send_type,receives,send_connect]
		//		[触发类型[]，发送方式，接收人[]，发送内容[]]
		//		
		foreach ($this->remindList as $arr) {
			//获取触发条件
			switch ($arr['trigger']) {
				case 0://新工单
					//创建时间判断  小于周期
					$where="TIMESTAMPDIFF(MINUTE,FROM_UNIXTIME(`add_time`),now())<=".$this->period*60;
					break;
				case 1://新回复
				//zy回复判断是否有新消息
					$hr="TIMESTAMPDIFF(MINUTE,FROM_UNIXTIME(`add_time`),now())<=".$this->period*60;
					$ids=Db::name('gongdan_xq')->where($hr)->column('gd_id');
					if(!empty($ids)){
						$ids=implode(",",array_unique($ids));
						$where="zy_xiaoxi=1 and id in (".$ids.")";
					}
					break;
				case 2://更改状态  在周期内更改状态
					$where="TIMESTAMPDIFF(MINUTE,FROM_UNIXTIME(`modify_time`),now())<=".$this->period*60;
					break;		
			}

			if(isset($where)){
				$gongdan=Db::table('cmf_gongdan')->where($where)->select();

				//格式化数据
				$this->formatSendList($gongdan,$arr);
			}
		}
	}

		/**
		 * 数据格式化
		 * @access protected
		 * @param  array $task 任务数据
		 * @param  array $arr  需要提醒的项
		 * @return 无
		 */
		protected function formatSendList($gongdan,$arr)
		{

			$title="";
			switch ($arr['trigger']) {
				case 0:
					$title="有新工单提交";
					break;
				case 1:
					$title="有新的工单回复";
					break;
				case 2:
					$title="工单状态更改提醒";
					break;		
			}
			$status=['关闭','开启'];
			foreach ($gongdan as $it) {
				//发送内容
				$send_connect="<div style='background-color:#ccc;margin-top:10px;'><h3>".$title."</h3>";
				$sms_connect=$title.":";
				foreach ($arr['send_connect'] as $k) {
					if($k==0){//
						$send_connect.="<div>标题：".$it['title']."</div>";
						$sms_connect.=$it['title'];
					}
					if($k==1){
						$send_connect.="<div>类型：".$it['type']."</div>";
					}
					if($k==2){
						$str=cmf_replace_content_file_url(htmlspecialchars_decode($it['content']));
						$send_connect.="<div>内容：".$str."</div>";
					}
				}

				if($arr['trigger']==1){
					//回复内容
					$where="userid=".$it['userid']." and gd_id=".$it['id']." and TIMESTAMPDIFF(MINUTE,FROM_UNIXTIME(`add_time`),now())<=".$this->period*60;
					$res=Db::name('gongdan_xq')->where($where)->column('content');
					foreach ($res as $k=>$v) {
						$str=cmf_replace_content_file_url(htmlspecialchars_decode($v));
						$send_connect.="<div>新回复内容：".$str."</div>";
					}
					
				}

				if($arr['trigger']==2){
					$sms_connect.=" 工单被".$status[$it['status']];
					$send_connect.="<div>状态：".$status[$it['status']]."</div>";
				}
				$send_connect.="</div>";
				$sms_connect.=" ";



				//接收人id 过滤数组去重复

				foreach ($arr['receives'] as $k=>$value) {//遍历接收人id
					foreach ($this->admin_users as $v) {//遍历后台员工
						if(intval($value) === $v['id']){//如果接收人id==后台员工id

							if($arr['send_type'] == 1 || $arr['send_type']==0){//邮件

								if(!isset($this->sendList['email'][$v['user_email']])){
									$this->sendList['email'][$v['user_email']]="";
								}	
								$this->sendList['email'][$v['user_email']]	.=	$send_connect;
							}
							if($arr['send_type'] == 2 || $arr['send_type']==0){//电话

								if(!isset($this->sendList['mobile'][$v['mobile']])){
									$this->sendList['mobile'][$v['mobile']]="";
								}	
								$this->sendList['mobile'][$v['mobile']]	.="(".$sms_connect.") ";
							}

						}

					}
				}
			}


		}





}